package com.baiyang.server.control.system;

import java.util.List;
import java.util.Map;

import com.baiyang.server.model.system.User;
import com.baiyang.server.tools.DataTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.model.system.UserRole;
import com.baiyang.server.service.system.UserRoleService;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/userRoleOperation")
@Jurisdiction(value = "用户角色信息}")
public class UserRoleCon {
	@Autowired
	private UserRoleService userRoleService = null;
	
	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "批量读取")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<UserRole> getPage(@RequestBody JSONObject object){
		Letter<UserRole> letter = new Letter<UserRole>();
		try {
			PageEntity<UserRole> result = userRoleService.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail(result.getResult());
			letter.setResult("SUCCESS");
			letter.setDetail("数据获取成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根据条件获取用户角色
	 * @param userRole
	 * @return
	 */
	@Jurisdiction(value = "selectList")
	@RequestMapping(value = "/selectList",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<UserRole> selectList(@RequestBody UserRole userRole){
		Letter<UserRole> letter = new Letter<UserRole>();
		try {
			List<UserRole> list = userRoleService.selectList(userRole);
			letter.setList(list);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根据条件获取一个用户信息
	 * @param userRole
	 * @return
	 */
	@Jurisdiction(value = "读取")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<UserRole> selectOne(@RequestBody UserRole userRole){
		Letter<UserRole> letter = new Letter<UserRole>();
		try {
			UserRole obj = userRoleService.selectOne(userRole);
			letter.setData(obj);
			letter.setDetail("SUCCESS");
			letter.setResult("数据获取成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 插入数据
	 * @param userRole
	 * @return
	 */
	@Jurisdiction(value = "新增")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody UserRole userRole){
		Letter<String> letter = new Letter<String>();
		userRole.setId(DataTool.createUUID());
		Map<String, String> sub = SubjectNotNull.subject(userRole);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！");
			return letter;
		}
		try {
			userRoleService.insert(userRole);
			letter.setDetail("数据新增成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 批量插入数据
	 * @param object
	 * @return
	 */
	@Jurisdiction(value = "insertBatch")
	@RequestMapping(value = "/insertBatch",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody JSONObject object){
		Letter<String> letter = new Letter<String>();
		try {
			String userId = object.getString("userId");
			if(DataTool.isBlank(userId)){
				letter.setResult("FAIL");
				letter.setDetail("数据不能为空！");
			}
			List<UserRole> list = object.getJSONArray("list").toJavaList(UserRole.class);
			userRoleService.DelAndInsertBatch(userId,list);
			letter.setDetail("数据插入成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝条件进行删除
	 * @param userRole
	 * @return
	 */
	@Jurisdiction(value = "删除")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody UserRole userRole){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(userRole)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			userRoleService.delete(userRole);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件进行删除
	 *
	 * @param userRoles
	 * @return
	 */
	@Jurisdiction(value = "deletes")
	@RequestMapping(value = "/deletes", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> deletes(@RequestBody List<UserRole> userRoles) {
		Letter<String> letter = new Letter<String>();
		if (DataTool.isBlank(userRoles)) {
			letter.setResult("FAIL");
			letter.setDetail("请至少选中一行！");
			return letter;
		}
		try {
			userRoleService.deletes(userRoles);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除成功！");
		}
		return letter;
	}
	
	/**
	 * 根绝id更新
	 * @param userRole
	 * @return
	 */
	@Jurisdiction(value = "修改")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody UserRole userRole){
		Letter<String> letter = new Letter<String>();
		if(userRole.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			userRoleService.updateById(userRole);
			letter.setDetail("数据修改成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据修改失败！");
		}
		return letter;	
	}
	
	
}
